const baseUrl = 'http://192.168.0.179:8080/wx'
/**
 * GET请求
 */
const GET = (params) => {
  return new Promise((resolve, reject) => {
    wx.request({
      ...params,
      url: baseUrl + params.url,
      method: 'GET',
      header: {
        'token': wx.getStorageSync('token'),
        'content-type': 'application/json', // 默认值
        'cache-control': 'no-cache',
      },
      success(res) {
        if (res.data.code == 401) {
          getOpenid()
          return false
        }
        resolve(res.data)
      },
      fail(res) {
        reject(res.data)
      }
    })
  })
}
/**
 * POST请求
 */
const POST = (params) => {
  return new Promise((resolve, reject) => {
    wx.request({
      ...params,
      url: baseUrl + params.url,
      method: 'POST',
      header: {
        'token': wx.getStorageSync('token'),
        'content-type': 'application/json;charset=UTF-8',
        'cache-control': 'no-cache'
      },
      success(res) {
        if (res.data.code == 401) {
          getOpenid()
          return false
        }
        resolve(res.data)
      },
      fail(res) {
        reject(res.data)
      }
    })
  })
}

/**
 * 上传
 */
const UploadFile = (params) => {
  return new Promise((resolve, reject) => {
    wx.uploadFile({
      ...params,
      url: baseUrl + params.url,
      header: {
        'token': wx.getStorageSync('token'),
        "Content-Type": "multipart/form-data",
        'cache-control': 'no-cache'
      },
      success(res) {
        if (res.data.code == 401) {
          getOpenid()
          return false
        }
        resolve(res.data)
      },
      fail(res) {
        reject(res.data)
      }
    })
  })
}

/**
 * 文件下载
 */
const Download = (params) => {
  return new Promise((resolve, reject) => {
    wx.downloadFile({
      url: baseUrl + params.url,
      header: {
        'token': wx.getStorageSync('token'),
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        resolve(res)
      },
      fail(res) {
        reject(res)
      }
    })
  })
}



module.exports = {
  GET,
  POST,
  Download,
  UploadFile
}